﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pacs.Services.Soa.Publishs
{

    public static  class Publisher
    {
        private static string HostIP = System.Configuration.ConfigurationManager.AppSettings["HostIP"];
        private static int HostPort =int.Parse(System.Configuration.ConfigurationManager.AppSettings["HostPort"]);
        private static RedisClient client ;

        public static void Init()
        {
            Task.Run(() =>
            {
                 client = new RedisClient("127.0.0.1", 62733);
                client.Ping();
                var subscription = client.CreateSubscription();
                subscription.OnMessage = (channel, msg) =>
                {
                };
                subscription.OnSubscribe = (channel) =>
                {
                };
                subscription.OnUnSubscribe = (a) => { Console.WriteLine("订阅客户端：取消订阅"); };
                subscription.SubscribeToChannels(new string[] { "Test" });
            });
        }
        public static void Publish(string JiGouID,string channel,string message)
        {
            client.PublishMessage(JiGouID+channel, message);
        }


        //public  class myCLient : RedisClient
        //{
        //    protected override 
        //}

        //public static void Publish()
        //{
        //    //PooledRedisClientManager
        //    IRedisClientsManager redisClientManager = new PooledRedisClientManager("127.0.0.1:6379");
        //    //发布、订阅服务 IRedisPubSubServer
        //    RedisPubSubServer pubSubServer = new RedisPubSubServer(redisClientManager, "channel1")
        //    {
        //        OnMessage = (channel, msg) =>
        //        {
        //            Console.WriteLine($"从频道：{channel}上接受到消息：{msg},时间：{DateTime.Now.ToString("yyyyMMdd HH:mm:ss")}");
        //            Console.WriteLine("___________________________________________________________________");
        //        },
        //        OnStart = () =>
        //        {
        //            Console.WriteLine("发布服务已启动");
        //            Console.WriteLine("___________________________________________________________________");
        //        },
        //        OnStop = () => { Console.WriteLine("发布服务停止"); },
        //        OnUnSubscribe = channel => { Console.WriteLine(channel); },
        //        OnError = e => { Console.WriteLine(e.Message); },
        //        OnFailover = s => { Console.WriteLine(s); },
        //    };
        //    //接收消息
        //    pubSubServer.Start();
        //}
    }
}
